<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: zzh
  Date: 2022/4/20
  Time: 4:40 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>院系管理</title>
</head>
<body>
<div>
    <center>
        <div>
            <div><h2>${title}</h2></div>
            <div>
                <div>院系管理</div>
                <div>
                    <button onclick="addCollage()">添加院系</button>
                </div>
                <div style="display: none" id="addCollageDiv">
                    <div><h4>添加院系</h4></div>
                    <div>
                        <table>
                            <tr>
                                <th>院系名称:</th>
                                <th><input type="text" id="addCollageText" value=""></th>
                            </tr>
                            <tr>
                                <th>学校:</th>
                                <th><select id="addSchoolId" value="0">
                                    <c:forEach items="${tSchoolList}" var="tSchool">
                                        <option value="${tSchool.schoolId}">${tSchool.schoolName}</option>
                                    </c:forEach>
                                </select></th>
                            </tr>
                        </table>
                        <div>
                            <button onclick="addCollageTrue()">确认添加</button>
                        </div>
                    </div>
                </div>
                <div>
                    <table border="1">
                        <tr>
                            <th>院系id</th>
                            <th>院系名称</th>
                            <th>学校名称</th>
                            <th>操作</th>
                        </tr>
                        <c:forEach items="${tCollageList}" var="tCollage">
                            <tr>
                                <td>${tCollage.collageId}</td>
                                <td>${tCollage.collageName}</td>
                                <td>${tCollage.schoolName}</td>
                                <td>
                                    <button onclick="changeCollage(${tCollage.collageId},'${tCollage.collageName}','${tCollage.schoolId}')">
                                        更改院系
                                    </button>
                                </td>
                            </tr>
                        </c:forEach>
                    </table>
                </div>
                <div style="display: none" id="changeCollageDiv">
                    <div>
                        <div><h4>修改院系</h4></div>
                        <div>
                            <table>
                                <tr>
                                    <th>院系名称:</th>
                                    <th><input type="text" id="changeCollageText"></th>
                                </tr>
                            </table>
                            <div>
                                <button onclick="changeCollageTrue()">确认修改</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </center>
</div>
</body>
<script type="text/javascript" src='<c:url value="/static/js/jquery.js" />'></script>
<script type="text/javascript">
    var collageId;
    var schoolId;

    function addCollage() {
        let addCollageDiv = document.getElementById("addCollageDiv");
        if (addCollageDiv.style.display === "block") {
            addCollageDiv.style.display = "none"
        } else {
            addCollageDiv.style.display = "block"
        }
    }

    function changeCollage(collageId1, collageName, schoolId1) {
        let changeCollageDiv = document.getElementById("changeCollageDiv");
        collageId = collageId1;
        schoolId = schoolId1;
        let changeCollageText = document.getElementById("changeCollageText");
        changeCollageText.value = collageName;
        if (changeCollageDiv.style.display === "block") {
            changeCollageDiv.style.display = "none"
        } else {
            changeCollageDiv.style.display = "block"
        }
    }

    function addCollageTrue() {
        let addCollageText = document.getElementById("addCollageText").value;
        let addSchoolId = document.getElementById("addSchoolId").value;
        if (addCollageText.length > 0 && addSchoolId > 0) {
            if (confirm("确认添加院系")) {
                var settings = {
                    "url": "${url}" + "/api/collage/add",
                    "method": "POST",
                    "timeout": 0,
                    "headers": {
                        "token": sessionStorage.getItem("token"),
                        "Content-Type": "application/json"
                    },
                    "data": JSON.stringify({
                        "collageName": addCollageText,
                        "schoolId": addSchoolId
                    }),
                };

                $.ajax(settings).done(function (response) {
                    if (response.status === 200) {
                        alert("添加成功");
                        history.go(0)
                    } else {
                        alert(response.data.error)
                    }
                });
            }
        } else {
            alert("请输入院系名称或选择学校")
        }
    }

    function changeCollageTrue() {
        let changeCollageText = document.getElementById("changeCollageText").value;
        if (changeCollageText.length > 0) {
            if (confirm("确认修改院系")) {
                var settings = {
                    "url": "${url}" + "/api/collage/change",
                    "method": "POST",
                    "timeout": 0,
                    "headers": {
                        "token": sessionStorage.getItem("token"),
                        "Content-Type": "application/json"
                    },
                    "data": JSON.stringify({
                        "collageId": collageId,
                        "collageName": changeCollageText,
                        "schoolId": schoolId
                    }),
                };

                $.ajax(settings).done(function (response) {
                    if (response.status === 200) {
                        alert("修改成功");
                        history.go(0)
                    } else {
                        alert(response.data.error)
                    }
                });
            }
        } else {
            alert("请输入院系名称")
        }
    }
</script>
</html>
